package parsing.productions;

import java.util.Random;

import parsing.InvalidSyntaxException;

public class MulOp extends MutableNode {
	// Invariant for children: empty.
	
	/**"*"*/
	public static final int TIMES  = 1;
	/**"/"*/
	public static final int DIVIDE = 2;
	/**mod*/
	public static final int MOD    = 3;
	
	private int type;
	
	/**
	 * Constructor: creates an instance of MulOp that is either TIMES, DIVIDE, or MOD.
	 * @param operation type of MulOp.
	 * Checks: operation is TIMES, DIVIDE, or MOD.
	 */
	public MulOp(int operation) {
		if (!(operation == TIMES || operation == DIVIDE || operation == MOD)) {
			throw new InvalidSyntaxException("The only types this MulOp can have are TIMES, DIVIDE, or MOD");
		}
		type= operation;
	}
	
	public int getType(){
		return type;
	}
	
	@Override
	public int size() {
		return 1;
	}

	@Override
	public void prettyPrint(StringBuffer sb) {
		switch(type){
		case TIMES: sb.append("* "); break;
		case DIVIDE: sb.append("/ "); break;
		case MOD: sb.append("mod "); break;
		}
	}

	/** Randomly changes this MulOp to a different type. */
	@Override
	public void mutate(Program genome) {
//		StringBuffer sb = new StringBuffer();
//		prettyPrint(sb);
//		sb.append("was changed to ");
		Random rnd = new Random();
		int newType;
		do {
			newType = rnd.nextInt(3) + 1;
		} while (type == newType);
		type = newType;
//		prettyPrint(sb);
//		System.out.println(sb);
	}

	@Override
	public Node duplicate() {
		return new MulOp(type);
	}

	/**
	 * Checks if this MulOp is the same as n
	 * @param n the node to check against
	 * @return true if n is a MulOp and its type is the same as this one's
	 */
	@Override
	public boolean equals(Node n){
		if(!(n instanceof MulOp))
			return false;
		MulOp mNode = (MulOp) n;
		if(type != mNode.type)
			return false;
		return true;
	}
}
